package com.zzl.rabbitmq.deadletter;

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.DeliverCallback;
import com.zzl.rabbitmq.utils.RabbitMqUtils;

import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.Map;

/**
 * Title: DeadLetterConsumer01.class<br>
 * Description: 死信队列消费者<br>
 * Copyright (c)  2021    <br>
 * Create DateTime: 2021/7/15 13:06 <br>
 *
 * @author zhuangzhuangliang
 */
public class DeadLetterConsumer01 {


    public static final String NORMAL_EXCHANGE ="normal_exchange";
    public static final String NORMAL_QUEUE ="normal_queue";
    public static final String DEAD_EXCHANGE ="dead_exchange";
    public static final String DEAD_QUEUE ="dead_queue";

    public static void main(String[] args) throws Exception{

        //声明交换机和队列
        Channel channel = RabbitMqUtils.getChannel();

        channel.exchangeDeclare(NORMAL_EXCHANGE,"direct");
        channel.exchangeDeclare(DEAD_EXCHANGE,"direct");

        //传入参数
        Map<String, Object> arguments = new HashMap<>();

        arguments.put("x-dead-letter-exchange",DEAD_EXCHANGE);
        arguments.put("x-dead-letter-routing-key","lisi");

        channel.queueDeclare(NORMAL_QUEUE,false,false,false,arguments);

        channel.queueDeclare(DEAD_QUEUE,false,false,false,null);

        channel.queueBind(NORMAL_QUEUE,NORMAL_EXCHANGE,"zhangsan");
        channel.queueBind(DEAD_QUEUE,DEAD_EXCHANGE,"lisi");

        DeliverCallback deliverCallback = (consumerTag,message)->{

            System.out.println("接收到消息: "+new String(message.getBody(), StandardCharsets.UTF_8));
        };
        channel.basicConsume(NORMAL_QUEUE,deliverCallback,consumerTag -> {});
    }
}
